package ganga.custodian.plot;

import java.text.DecimalFormat;
import java.util.Vector;

import ganga.custodian.CustodianApplication;
import ganga.custodian.R;

import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;

import com.androidplot.ui.layout.AnchorPosition;
import com.androidplot.ui.layout.DynamicTableModel;
import com.androidplot.ui.layout.XLayoutStyle;
import com.androidplot.ui.layout.YLayoutStyle;
import com.androidplot.xy.LineAndPointFormatter;
import com.androidplot.xy.LineAndPointRenderer;
import com.androidplot.xy.XYPlot;
import com.androidplot.xy.XYStepMode;

public class HeartPlotData {
	public XYPlot heartPlot;
	
	static int FIRST=9;
	public KIGSeries kigSeries;
	public BMPSeries bmpSeries;
	public LineAndPointFormatter colorKigSeries;
	public LineAndPointFormatter colorbmpSeries;
	public double minY;
	public double maxY;
	public double MINY;
	public double MAXY;
	public int selectSeries;
	public double[] arrayOfInterval;

	public HeartPlotData(double[] arrayOfInterval) {
		this.selectSeries = FIRST;
		this.arrayOfInterval = arrayOfInterval;
	}

	public void switchSeries(){
	    if (selectSeries == 0){
	        heartPlot.removeSeries(bmpSeries, LineAndPointRenderer.class);
	        heartPlot.addSeries(kigSeries, LineAndPointRenderer.class, colorKigSeries);
	        selectSeries = 1;
	        heartPlot.setDomainStep(XYStepMode.INCREMENT_BY_VAL, Math.round(kigSeries.getMaxX() / 1000) * 20);
	        heartPlot.setRangeStep(XYStepMode.INCREMENT_BY_VAL, 10);
	        heartPlot.setRangeLabel(getString(R.string.RRInterval));
	        maxY = kigSeries.getMaxX();
	        MAXY = kigSeries.getMaxX();
	        // mySimpleXYPlot.setRangeBoundaries(kigSeries.getMinY()-5, kigSeries.getMaxY()+5, BoundaryMode.AUTO);
	        heartPlot.redraw();
	        return;
	    }
	    else{
	        if (selectSeries != 9)
	            heartPlot.removeSeries(kigSeries, LineAndPointRenderer.class);
	        heartPlot.addSeries(bmpSeries, LineAndPointRenderer.class, colorbmpSeries);
	        selectSeries = 0;
	        heartPlot.setDomainStep(XYStepMode.INCREMENT_BY_VAL, Math.round(bmpSeries.getMaxX() / 1000) * 20);
	        heartPlot.setRangeStep(XYStepMode.INCREMENT_BY_VAL, 1);
	        heartPlot.setRangeLabel(getString(R.string.BeatsPerMinute));
	        maxY = bmpSeries.getMaxX();
	        MAXY = bmpSeries.getMaxX();
	        // mySimpleXYPlot.setRangeBoundaries(bSeries.getMinY()-5, bSeries.getMaxY()+5, BoundaryMode.AUTO);
	        heartPlot.redraw();
	        return;
	    }
	}

	void buildXYPlot(){
	    heartPlot.getGraphWidget().setTicksPerRangeLabel(2);
	    heartPlot.getGraphWidget().setTicksPerDomainLabel(5);
	    heartPlot.getGraphWidget().setRangeValueFormat(new DecimalFormat("#####"));
	    heartPlot.getGraphWidget().setDomainValueFormat(new DecimalFormat("#####"));
	    heartPlot.getGraphWidget().setRangeLabelWidth(25);
	    heartPlot.setDomainLabel(getString(R.string.TimeLabel));
	    heartPlot.disableAllMarkup();
	    colorbmpSeries = new LineAndPointFormatter(Color.rgb(0, 155, 255), Color.rgb(80, 0, 255),
	        Color.argb(150, 0, 0, 150));
	    colorKigSeries = new LineAndPointFormatter(Color.rgb(255, 200, 0), Color.rgb(200, 255, 0),
	        Color.argb(100, 0, 255, 100));
	 
	    Paint lineFill = new Paint();
	    lineFill.setAlpha(400);
	    lineFill.setShader(new LinearGradient(0, 0, 0, 250, Color.WHITE, Color.BLUE, Shader.TileMode.MIRROR));
	    colorbmpSeries.setFillPaint(lineFill);
	    Paint lineKigFill = new Paint();
	    lineKigFill.setAlpha(400);
	    lineKigFill.setShader(new LinearGradient(0, 0, 0, 250, Color.WHITE, Color.YELLOW, Shader.TileMode.MIRROR));
	    colorKigSeries.setFillPaint(lineKigFill);
	 
	    minY = 0;
	    MINY = 0;
	    heartPlot.getLegendWidget().setTableModel(new DynamicTableModel(1, 1));
	    heartPlot.position(heartPlot.getDomainLabelWidget(), 190, XLayoutStyle.ABSOLUTE_FROM_RIGHT, -4,
	        YLayoutStyle.ABSOLUTE_FROM_BOTTOM, AnchorPosition.RIGHT_BOTTOM);
	    heartPlot.position(heartPlot.getLegendWidget(), -160, XLayoutStyle.ABSOLUTE_FROM_RIGHT, 203,
	        YLayoutStyle.ABSOLUTE_FROM_BOTTOM, AnchorPosition.RIGHT_BOTTOM);
	    kigSeries = new KIGSeries(arrayToVector(arrayOfInterval));
        bmpSeries = new BMPSeries(arrayToVector(arrayOfInterval));
        kigSeries.setLabel(getString(R.string.KIG));
        bmpSeries.setLabel(getString(R.string.BMP));
	}

	private String getString(int resId){
		return CustodianApplication.getInstance().getString(resId);
	}

	public Vector<Double> arrayToVector(double[] array) {
		Vector<Double> localVector = new Vector<Double>();

		for (int i = 0; i < array.length; i++) {
			double y = array[i];
			localVector.add(y);
		}
		return (localVector);
	}

	public Vector<Double> partArrayToVector(double[] array, int strt, int end) {
		int i;
		Vector<Double> localVector = new Vector<Double>();

		for (i = strt; i <= end; i++) {
			double y = array[i];
			localVector.add(y);
		}
		return localVector;
	}
}